<?php
namespace app\index\controller;

use controller\BasicIndex;
use think\Model;
use think\Db;
use think\View;

/**
 * 前台网站入口
 *
 * @author leolei <346991581@qq.com>
 */
class Index extends BasicIndex
{

    /**
     * 网站入口
     */
    public function index()
    {
        //协会介绍
        $this->assign('pics', model('YogaArticle')->getlist(['type'=>2], 'id asc', 0, 3));
        //协会介绍
        $this->assign('about', model('YogaArticle')->getinfo(['type'=>1,'cid'=>1]));
        //查询公告
        $this->assign('notices', model('YogaArticle')->getlist(['type'=>0,'cid'=>16], 'id asc', 0, 6));
        //评审导师
        $this->assign('teachers', model('YogaArticle')->getlist(['type'=>0,'cid'=>3], 'id asc', 0, 8));
        //培训基地
        $this->assign('trainings', model('YogaArticle')->getlist(['type'=>0,'cid'=>20], 'id asc', 0, 8));
        //媒体报道
        $this->assign('medias', model('YogaArticle')->getlist(['type'=>0,'cid'=>17], 'id asc', 0, 8));
        //职场攻略
        $this->assign('tastes', model('YogaArticle')->getlist(['type'=>0,'cid'=>18], 'id asc', 0, 8));
        //外国新闻
        $this->assign('foreigns', model('YogaArticle')->getlist(['type'=>0,'cid'=>19], 'id asc', 0, 8));
        return view();
    }

    /**
     * 证书查询
     *
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function auth()
    {
        $where['idcard'] = input('post.idcard');
        $where['auth_sn'] = input('post.auth_sn');
        $pic = Db::name('YogaAuth')->where($where)->value('pic');
        if ($pic) {
            $this->success('查询成功', $pic);
        } else {
            $this->error('查询失败,此证书不存在');
        }
    }

    /**
     * 单页文章
     *
     * @param string $url
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function page($url)
    {
        $cate = Db::name('YogaCategory')->where(['is_deleted'=>0,'url'=>'/'.$url])->field('id,type')->find();
        switch ($cate['type']) {
            case '0':
                self::news($cate['id']);
                return view('pic');
                break;
            case '1':
                $info = model('YogaArticle')->getinfo(['type'=>1,'cid'=>$cate['id']]);
                $this->assign('title', $info['title']);
                $this->assign('info', $info);
                return view();
                break;
            case '2':
                $ids = Db::name('YogaCategory')->where(['is_deleted'=>0,'pid'=>$cate['id']])->column('id');
                $p = input('get.p')?input('get.p'):'1';
                $info = model('YogaArticle')->newslist(['type'=>0,'cid'=>['in',implode(',', $ids)]], 'id desc', $p, 12);
                $info['title'] = Db::name('YogaCategory')->where(['is_deleted'=>0,'id'=>$cate['id']])->value('title');
                $this->assign('info', $info);
                return view('news');
                break;
        }
    }

    /**
     * 新闻列表页
     *
     * @param string $cid
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function news_list($url)
    {
        $cate = Db::name('YogaCategory')->where(['is_deleted'=>0,'url'=>'/news/'.$url])->field('id,type')->find();
        self::news($cate['id']);
        return view('news');
    }

    /**
     * 新闻列表大类
     *
     * @param string $url
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    protected function news($cid)
    {
        $p = input('get.p')?input('get.p'):'1';
        $info = model('YogaArticle')->newslist(['type'=>0,'cid'=>$cid], 'id desc', $p, 12);
        $info['title'] = Db::name('YogaCategory')->where(['is_deleted'=>0,'id'=>$cid])->value('title');
        $this->assign('title', $info['title']);
        $this->assign('info', $info);
    }

    /**
     * 新闻内容页
     *
     * @param string $id
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function view($id)
    {
        $info = model('YogaArticle')->getinfo(['type'=>0,'id'=>$id]);
        $this->assign('title', $info['title']);
        $this->assign('cate', Db::name('YogaCategory')->where(['is_deleted'=>0,'id'=>$info['cid']])->find());
        $this->assign('info', $info);
        return view();
    }
}
